<?php
  include_once './lib/fun.php';
  if (checkLogin()) {
      if (!empty($_POST['name'])) {
          if (!empty($_POST['id'])) {
              $id = $_POST['id'];
              $con = mysqlInit('127.0.0.1', 'root', 'root', 'mall');
              $sql = "SELECT * FROM `mall_goods` WHERE `id`='{$id}'";
              $res = mysql_query($sql);
              $goods = mysql_fetch_assoc($res);
              if ($goods) {
                  $name = mysql_real_escape_string(trim($_POST['name']));
                  $price = intval($_POST['price']);
                  $des = mysql_real_escape_string(trim($_POST['des']));
                  $content = mysql_real_escape_string(trim($_POST['content']));
                  $file = $_FILES['file'];
                  $nameLength = mb_strlen($name, 'utf-8');
                  $now = $_SERVER['REQUEST_TIME'];
                  if ($nameLength<0 || $nameLength>30) {
                      toMsgPage(2, '画品名称需要在30个字符之间');
                  }
                  if ($price<0 || $price>999999999) {
                      toMsgPage(2, '画品价格应少于999999999');
                  }
                  $desLength = mb_strlen($des, 'utf-8');
                  if ($desLength<0|| $desLength> 100) {
                      toMsgPage(2, '简介需要在100个字符之间');
                  }
                  if (empty($content)) {
                      toMsgPage(2, '画品详情不能为空');
                  }
                  $uploadData = array(
                    'name' => $name,
                    'price' => $price,
                    'des' => $des,
                    'content' => $content
                  );
                  $sqlData = '';
                  foreach ($uploadData as $key => $value) {
                      if ($goods[$key] == $value) {
                          unset($uploadData[$key]);
                      } else {
                          $sqlData .= "`{$key}` = '{$value}' ,";
                      }
                  }

                  if ($file['size'] > 0) {
                      $pic = uploadFile($file);
                      $sqlData .="`pic`='{$pic}',";
                  }
                  if (!empty($uploadData)) {
                      $sqlData .= "`update_time` = '{$now}'";
                      // $sqlData = rtrim($sqlData, ',');
                      unset($sql,$res);
                      $updateSql = "UPDATE `mall_goods` SET {$sqlData} WHERE `id` = {$id}";
                      $updateRes = mysql_query($updateSql);
                      $rows = mysql_affected_rows();//影响行数
                      if ($rows>=1) {
                          toMsgPage(1, '更新成功', "edit.php?id={$id}");
                      } else {
                          toMsgPage(2, mysql_error());
                      }
                  } else {
                      toMsgPage(1, '没有更新内容');
                  }
              } else {
                  toMsgPage(2, '不存在该商品');
              }
          } else {
              toMsgPage(2, '不存在该商品');
          }
      } else {
          toMsgPage(2, '路由非法', 'edit.php');
      }
  } else {
      toMsgPage(2, '请先进行登录再进行编辑', 'login.php');
  }
